package com.loe.ktui.custom

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.widget.ViewPager2
import com.loe.ktui.MATCH_PARENT
import com.loe.ktui.R
import com.loe.ktui.dist
import com.loe.ktui.ktSafe
import com.loe.ktui.KtFrameLayout
import com.loe.ktui.UiFrameLayout
import com.loe.ktui.UiGroup
import com.loe.ktui.UiView
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import com.zhpan.bannerview.constants.IndicatorGravity
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.bannerview.transform.ScaleInTransformer
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle

class BannerBean(
    var imageUrl: String = "",
    var linkUrl: String = "",
    var name: String = "",
    var extra: String = "",
)

open class UiBannerView(id: String? = null) : UiView(id)
{
    override var width: Any? = MATCH_PARENT

    var interval: Int = 6000
    var scrollDuration: Int = 600
    var autoPlay: Boolean = true
    var canLoop: Boolean? = null
    var indicatorVisible: Boolean? = null
    var indicatorColor0: Int = 0x88ffffff.toInt()
    var indicatorColor1: Int = 0xFFffffff.toInt()
    var indicatorWidth0: Any = 4
    var indicatorWidth1: Any = 4
    var indicatorRadius0: Any = 4
    var indicatorRadius1: Any = 4
    var indicatorGap: Any = 4
    var indicatorHeight: Any = 4

    var indicatorMargin: Any? = null
    var indicatorMarginLeft: Any? = null
    var indicatorMarginTop: Any? = null
    var indicatorMarginRight: Any? = null
    var indicatorMarginBottom: Any? = 8

    var indicatorGravity: Int = IndicatorGravity.CENTER
    var indicatorMode: Int = IndicatorSlideMode.WORM
    var indicatorStyle: Int = IndicatorStyle.ROUND_RECT
    var pageStyle: Int = PageStyle.NORMAL
    var pageStyleScale: Float = ScaleInTransformer.DEFAULT_MIN_SCALE

    var leftRevealWidth: Any? = null
    var rightRevealWidth: Any? = null
    var pageMargin: Any = 0

    var itemUI: UiFrameLayout? = null

    var itemConvert: (KtFrameLayout.(bean: BannerBean, i: Int) -> Unit)? = null
    fun itemConvert(itemConvert: KtFrameLayout.(bean: BannerBean, i: Int) -> Unit)
    {
        this.itemConvert = itemConvert
    }

    var onPageSelect: (KtBannerView.(bean: BannerBean, i: Int) -> Unit)? = null
    fun onPageSelect(onPageSelect: KtBannerView.(bean: BannerBean, i: Int) -> Unit)
    {
        this.onPageSelect = onPageSelect
    }

    var datas: List<BannerBean>? = null

    open fun initUI(context: Context, view: KtBannerView)
    {
        corner?.let { view.setRoundCorner(it.dist) }
        corner = null

        super.initUI(context, view)

        view.setInterval(interval)
        view.setScrollDuration(scrollDuration)
        view.setAutoPlay(autoPlay)
        canLoop?.let { view.setCanLoop(it) }
        indicatorVisible?.let { view.setIndicatorVisibility(if (it) View.VISIBLE else View.GONE) }
        view.setIndicatorSliderColor(indicatorColor0, indicatorColor1)
        view.setIndicatorSliderWidth(indicatorWidth0.dist, indicatorWidth1.dist)
        view.setIndicatorSliderRadius(indicatorRadius0.dist, indicatorRadius1.dist)
        view.setIndicatorSliderGap(indicatorGap.dist)
        view.setIndicatorHeight(indicatorHeight.dist)
        if (indicatorMargin != null)
        {
            val d = indicatorMargin.dist
            view.setIndicatorMargin(d, d, d, d)
        } else
        {
            view.setIndicatorMargin(indicatorMarginLeft.dist, indicatorMarginTop.dist, indicatorMarginRight.dist, indicatorMarginBottom.dist)
        }
        view.setIndicatorGravity(indicatorGravity)
        view.setIndicatorSlideMode(indicatorMode)
        view.setIndicatorStyle(indicatorStyle)
        view.setPageStyle(pageStyle, pageStyleScale)
        if (leftRevealWidth != null || rightRevealWidth != null)
        {
            view.setRevealWidth(leftRevealWidth.dist, rightRevealWidth.dist)
        }
        view.setPageMargin(pageMargin.dist)

        view.itemUI = itemUI

        ktSafe { view.setLifecycleRegistry((context as FragmentActivity).lifecycle) }
        itemConvert?.let { view.setItemConvert(it) }
        onPageSelect?.let { view.setOnPageSelect(it) }
        datas?.let { view.refreshData(it.toList()) }

        view.create()

    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtBannerView
    {
        return KtBannerView(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_BannerView(id: String? = null, block: (UiBannerView.() -> Unit)? = null): UiBannerView
{
    val ui = UiBannerView(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

open class KtBannerView : BannerViewPager<BannerBean>
{
    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(attrs)
    }

    private fun initXML(attrs: AttributeSet? = null)
    {
        registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback()
        {
            override fun onPageSelected(i: Int)
            {
                onPageSelect?.invoke(this@KtBannerView, data[i], i)
            }
        })

        adapter = object : BaseBannerAdapter<BannerBean>()
        {
            override fun getLayoutId(viewType: Int) = R.layout.kt_item_ui_full

            override fun bindData(holder: BaseViewHolder<BannerBean>, bean: BannerBean, position: Int, pageSize: Int)
            {
                val vg = holder.itemView as KtFrameLayout
                if (itemUI != null && vg.childCount == 0)
                {
                    itemUI?.initUI(context, vg)
                }
                itemConvert?.invoke(vg, bean, position)
            }
        }
    }

    /********************************************************************************************/

    var itemUI: UiFrameLayout? = null

    internal var itemConvert: (KtFrameLayout.(bean: BannerBean, i: Int) -> Unit)? = null

    /** 设置自定义Item处理 */
    fun setItemConvert(itemConvert: (KtFrameLayout.(bean: BannerBean, i: Int) -> Unit)?)
    {
        this.itemConvert = itemConvert
    }

    private var onPageSelect: (KtBannerView.(bean: BannerBean, i: Int) -> Unit)? = null

    fun setOnPageSelect(onPageSelect: KtBannerView.(bean: BannerBean, i: Int) -> Unit)
    {
        this.onPageSelect = onPageSelect
    }
}